<template>
  <view>
		
		<top-bar fixed="relative"></top-bar>
    <view class="item" v-for="(address,index) in addressList" :key="index" @tap="useAddress(address)">
      <view class="top">
        <view class="name">{{ address.username }}</view>
        <view class="phone">{{ address.tel }}</view>
        <u-tag class="u-m-l-20" text="默认" mode="light" v-if="address.isDefault === 'T'" type="error" />
      </view>
      <view class="bottom">
        {{ address.address }}
        <view @tap.stop="jump('/pages/address/addressEdit',address)">
          <u-icon name="edit-pen" :size="40" color="#999999"></u-icon>
        </view>
      </view>
    </view>
    <view class="addSite"  @tap="jump('/pages/address/addressEdit',{addressId:'-1'})">
      <view class="add">
        <u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建地址
      </view>
    </view>
  </view>
</template>

<script>
import {
  getUserAddress
} from '../../api/user/userApi.js'
import UButton from "../../uview-ui/components/u-button/u-button"
	import topBar from "@/components/top-bar/top-bar.vue";
export default {
  components: {UButton, topBar},
  data() {
    return {
      addressList: [],
      from: '',
      platform: uni.getStorageSync('platform')
    };
  },
  computed: {},
   async onLoad(options) {
		 await this.vc.onLoad(options).then(_suc=>{},_err=>{});
  },
  onShow() {
    this.init();
  },
  methods: {
    init() {
      return Promise.all([this.getAddressList()]);
    },
    useAddress(address) {
      let pages = getCurrentPages();

      // #ifdef MP-WEIXIN || APP-PLUS
      let currPage = pages[pages.length - 1].$vm;
      let prevPage = pages[pages.length - 2].$vm; //上一个页面
      // #endif

      // #ifdef H5
      let currPage = pages[pages.length - 1];
      let prevPage = pages[pages.length - 2]; //上一个页面
      // #endif
      prevPage.address = address;
	  
	  uni.$emit('userAddress',address);

      uni.navigateBack({
        delta: 1
      });
    },
    // 路由跳转
    jump(path, param) {
      this.vc.navigateTo({
        url: path + '?addressId=' + param.addressId
      })
    },
    getAddressList() {
      let that = this;

      let param = {
        page: 1,
        row: 30,
        userId: this.vc.getCurrentUser().userId
      }
      getUserAddress(param).then((data) => {
        that.addressList = data.data;
      });

    }
  }
};
</script>

<style lang="scss" scoped>
.item {
  padding: 40rpx 20rpx;
  background-color: white;
  border-bottom: solid #000000 0d;
  .top {
    display: flex;
    font-weight: bold;
    font-size: 34rpx;
    .phone {
      margin-left: 60rpx;
    }
    .tag {
      display: flex;
      font-weight: normal;
      align-items: center;
      text {
        display: block;
        width: 60rpx;
        height: 34rpx;
        line-height: 34rpx;
        color: #ffffff;
        font-size: 20rpx;
        border-radius: 6rpx;
        text-align: center;
        margin-left: 30rpx;
        background-color:rgb(49, 145, 253);
      }
      .red{
        background-color:red
      }
    }
  }
  .bottom {
    display: flex;
    margin-top: 20rpx;
    font-size: 28rpx;
    justify-content: space-between;
    color: #999999;
  }
}
.addSite {
  display: flex;
  justify-content: space-around;
  width: 600rpx;
  line-height: 100rpx;
  position: absolute;
  bottom: 30rpx;
  left: 80rpx;
  background-color: red;
  border-radius: 60rpx;
  font-size: 30rpx;
  .add{
    display: flex;
    align-items: center;
    color: #ffffff;
    .icon{
      margin-right: 10rpx;
    }
  }
}
</style>
